package com.pro.controller;

import com.pro.common.GuavaCommon;
import com.pro.pojo.JsActicleCategroy;
import com.pro.pojo.JsComment;
import com.pro.pojo.JsonData;
import com.pro.service.JsActicleCategroyService;
import com.pro.utils.GuavaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@RequestMapping("/JsActicleCategroy")
@Controller
public class JsActicleCategroyController {
    @Autowired
    JsActicleCategroyService jacs;
    //创建缓存工具类对象
    @Autowired
    GuavaUtils guavaUtils;
    //查询文章分类
    @RequestMapping("/getAllJsActicleCategroy")
    @ResponseBody
    public JsonData getAllJsActicleCategroy(){
        //获取缓存中是否存在数据
        Object ifPresent = guavaUtils.getTenCacheBuild().getIfPresent(String.valueOf(GuavaCommon.ArticleClassification));
        //第一次缓存中没有数据走else
        List<JsActicleCategroy> allJsActicleCategroy;
        if (ifPresent != null){
            return ifPresent!=null?JsonData.buildSuccess(ifPresent):JsonData.buildError("服务器出错了");
        }else{
            allJsActicleCategroy = jacs.getAllJsActicleCategroy();
            //缓存对象调用方法        再调用put将枚举类中的枚举对象当作key   方法当作value 将查询出的数据添加到枚举对象中
            guavaUtils.getTenCacheBuild().put(String.valueOf(GuavaCommon.ArticleClassification),jacs.getAllJsActicleCategroy());
        }
        return allJsActicleCategroy != null?JsonData.buildSuccess(allJsActicleCategroy):JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/addJsActicleCategroy")
    @ResponseBody
    public JsonData addJsActicleCategroy(String artcleCategroyName, Integer parentId,Integer createId,String createDate){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ArticleClassification));
        boolean b = jacs.addJsActicleCategroy(artcleCategroyName, parentId,createId,createDate);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/editJsActicleCategroy")
    @ResponseBody
    public JsonData editJsActicleCategroy(String artcleCategroyName,Integer acticleCategroyId,Integer updateId,String updateDate){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ArticleClassification));
        boolean b = jacs.editJsActicleCategroy(artcleCategroyName, acticleCategroyId,updateId,updateDate);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/deleteJsActicleCategroy")
    @ResponseBody
    public JsonData deleteJsActicleCategroy(Integer acticleCategroyId){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ArticleClassification));
        boolean b = jacs.deleteJsActicleCategroy(acticleCategroyId);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
}
